<extend name="./public/frame.html"/>
<block name="content">
    <blockquote class="layui-elem-quote search">
        <div class="layui-inline">
            <select name="province" lay-filter="province_id" id="province_id" class="layui-input" style="width: 120px;" onchange="provinceChange();">
                <option value="">请选择省份</option>
                <volist name="province" id="vo">
                    <option value="{$vo['region_id']}">{$vo['region_name']}</option>
                </volist>
            </select>
        </div>
        <div class="layui-inline">
            <select name="city" id="city_id" class="layui-input" lay-filter="city_id" style="width: 120px;" onchange="cityChange();">
                <option value="">请选择城市</option>
            </select>
        </div>
        <div class="layui-inline">
            <select name="country" id="country_id" class="layui-input" lay-filter="country_id" style="width: 120px;">
                <option value="">请选择地区</option>
            </select>
        </div>
        <div class="layui-inline">
            <input type="text" name="name" lay-verify="required|name" required placeholder="车队名称" class="layui-input" id="name">
        </div>
        <div class="layui-inline">
            <button class="layui-btn" id="search" data-type="search">搜索</button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-normal" data-url="{:url('add')}" onclick="add_t(this,'添加车队')">
                添加车队
            </button>
            <button class="layui-btn layui-btn-warm" id="excel" data-type="excel">导出数据</button>
        </div>
    </blockquote>

    <table class="layui-table" lay-data="{width:'auto', height:'auto', url:'{:url('Business/index')}', page: true, limit: 10, id:'Business'}" lay-filter="Business">
        <thead>
        <tr>
            <th lay-data="{checkbox:true, fixed: true}"></th>
            <th lay-data="{field:'id', width:100,align:'center', fixed: true, sort: true}">车队ID</th>
            <th lay-data="{field:'province_name', width:150,align:'center'}">省份</th>
            <th lay-data="{field:'city_name', width:150,align:'center'}">城市</th>
            <th lay-data="{field:'country_name', width:150,align:'center'}">区域</th>
            <th lay-data="{field:'name', width:150,align:'center'}">车队名称</th>
            <th lay-data="{field:'username', width:150,align:'center'}">联系人</th>
            <th lay-data="{field:'phone', width:150,align:'center'}">手机号</th>
            <th lay-data="{field:'reg_num', width:120,align:'center'}">扫码注册人数</th>
            <!--<th lay-data="{field:'first_login_num', width:150,align:'center'}">首次登录人数(wap)</th>-->
            <th lay-data="{field:'first_num', width:150,align:'center'}">完成首单人数</th>
            <!--<th lay-data="{field:'total_money', width:150,align:'center'}">累计提成金额</th>
            <th lay-data="{field:'balance', width:150,align:'center'}">余额</th>
            <th lay-data="{field:'settled_money', width:150,align:'center'}">已结算金额</th>-->
            <th lay-data="{fixed:'right', toolbar: '#barDemo', width:300, align:'center'}">操作</th>
        </tr>
        </thead>
    </table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit" style="width: 50px">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" style="width: 50px">删除</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="code" style="width: 80px">生成二维码</a>
    </script>
    <script>
        layui.use('table', function () {
            var table = layui.table;
            table.on('tool(Business)', function (obj) {
                var data = obj.data;
                var id = data.id;
                if (obj.event === 'del') {
                    layer.confirm('确定删除吗?',function(index){
                        var url = '{:url("/admin/Business/del","id=")}' + id
                        $.get(url,function(data){
                            if(data.status == 1){
                                layer.msg('成功删除',{'icon':1});
                                window.location.reload();
                            }else{
                                layer.msg('操作失败',{'icon':2});
                            }
                        },'json')
                        layer.close(index);
                    })

                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: '车队修改',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '{:url("/admin/Business/edit","id=")}' + id //iframe的url
                    });

                } else if(obj.event === 'code'){
                    layer.open({
                        type: 2,
                        title: '生成二维码',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['600px', '40%'],
                        content: '{:url("/admin/Business/refer_code","id=")}' + id //iframe的url
                    });
                }
            });
            //搜索
            $('#search').on('click', function () {
                var name = $("#name").val();
                var province_id = $('#province_id').val();			//省份
                var city_id = $('#city_id').val();
                var country_id = $('#country_id').val();
                table.reload('Business', {
                    url: '/admin/Business/index', where: {
                        "name": name,
                        "province_id": province_id,
                        "city_id": city_id,
                        "country_id": country_id
                    }
                });
            })
        });

        $("#excel").on('click',function () {
            var headers = {
                'id': '车队ID',
                'province_name': '省份',
                'city_name': '城市',
                'country_name': '区域',
                'name': '车队名称',
                'username':'联系人',
                'phone': '手机号',
                'reg_num': '扫码注册人数',
                'first_num': '完成首单人数',
            };
            var header_str = JSON.stringify(headers);
            var name = $("#name").val();
            var province_id = $('#province_id').val();
            var city_id = $('#city_id').val();
            var country_id = $('#country_id').val();
            var str = "&name=" + name + "&province_id=" + province_id + "&city_id=" + city_id + "&country_id=" + country_id;
            layer.confirm('请选择导出内容', {
                btn: ['全部数据','取消'] //可以无限个按钮
                , btn3: function (index,layero) {
                    layer.close(index);
                }
            }, function (index) {
                //按钮【按钮一】的回调
                location = "{:url('excel','type=1&headers=')}" + header_str + str;
                layer.close(index);
            });
        });

        function provinceChange() {
            var province_id = $("#province_id").val();
            if(province_id == 0) {
                if(province_id == 0){
                    $("#city_id").empty();
                    $("#city_id").append('<option value=>请选择城市</option>');
                    return false;
                }
            }
            $.ajax({
                type: 'POST',
                url: "{:url('/admin/Franchisee/cityList')}",
                data: {'province_id':province_id},
//                dataType:  'json',
                success:function(res){
                    $("select[name='city']").empty();
                    $("#city_id").append("<option value=''>全部城市</option>" + res);
                },
                error:function (res) {
                    layer.msg('请求错误');return false;
                }
            });
        }

        function cityChange() {
            var city_id = $("#city_id").val();
            if(city_id == 0) {
                if(city_id == 0){
                    $("#country_id").empty();
                    $("#country_id").append('<option value=>请选择地区</option>');
                    return false;
                }
            }
            $.ajax({
                type: 'POST',
                url: "{:url('getCountryList')}",
                data: {'city_id':city_id},
//                dataType:  'json',
                success:function(res){
                    $("select[name='country']").empty();
                    $("#country_id").append("<option value=''>全部地区</option>" + res);
                },
                error:function (res) {
                    layer.msg('请求错误');return false;
                }
            });
        }
    </script>
</block>
